package com.itheima.manager.dao;

import com.itheima.manager.domain.DitieZhan;
import com.itheima.manager.domain.ManagerMan;
import com.itheima.manager.utils.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class DiTieDao {
    private static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    private static   ArrayList<DitieZhan> dtlist = new ArrayList<DitieZhan>();
    private static   ArrayList<ManagerMan> mglist = new ArrayList<ManagerMan>();

    //查找地铁信息
    public ArrayList<DitieZhan> selectDiTieZhan() {
        dtlist.clear();
        String sql = "select * from ditie";
        List<Map<String, Object>> list = template.queryForList(sql);
        for (Map<String, Object> objectMap : list) {
            DitieZhan ditieZhan = new DitieZhan((String) objectMap.get("sid"), (String) objectMap.get("name"), (String) objectMap.get("line"));
            dtlist.add(ditieZhan);
        }
        return dtlist;
    }

    //增加一条地铁信息
    public boolean addDiTieZhan(DitieZhan dt) {
        String sql = "insert into ditie(sid,name,line) values(?,?,?) ";
        int update = template.update(sql, dt.getId(), dt.getName(), dt.getLine());
        if (update > 0) {
            System.out.println("插入数据成功:  一共插入" + update);
        }
        return true;
    }

    //删除地铁信息
    public void deleteDiTieZhan(String id) {
        String sql = "delete from ditie where sid=  ? ";
        int update = template.update(sql, id);
        if (update > 0) {
            System.out.println("删除数据成功:  一共删除" + update);
        }else {
            System.out.println("======删除信息失败======");
        }
    }

    //修改地铁信息
    public void updateDiTieZhan(String id, DitieZhan dt) {
        String sql = "update ditie set  name = ? , line= ?  where sid= ? ";
        int update = template.update(sql,dt.getName(), dt.getLine(), dt.getId());
        if (update > 0) {
            System.out.println("修改数据成功:  一共删除" + update);
        } else {
            System.out.println("======删除失败======");
        }
    }
}
